import { Component, OnInit } from '@angular/core';
import { GridOptions } from '@ag-grid-community/core';
import { GridConfigService } from '@core/config/grid-config.service';
import { ApiService } from '@core/services/http/api.service';
import { UserAuthService } from '@core/services/user-auth.service';
import {ApesMessageService} from "@shared/apes/message";

@Component({
  selector: 'testing',
  templateUrl: './testing.component.html',
  styles: [`
    * {
    -webkit-overflow-scrolling: touch;
    -webkit-transform: translateZ(0);
  }
  `]
})
export class TestingComponent implements OnInit {
  /* 按钮相关 */
  buttonBar = [
    {hide: true, style: 'default', label: '刷新', id: 'action_query'}
  ];
  title = '';
  ctx = {model: {}};

  /** Grid 相关 **/
  resultData:any;
  gridOptions:GridOptions;
  pinnedBottomRowData:any[] = [];
  columnDefs:any[] = [];
  getContextMenuItems:any;
  height = 275 + 'px';
  posts :any ;                // 获取登录角色所在门店ID

  constructor(private apiService:ApiService,
              private gridConfigService:GridConfigService,
              private userAuthService: UserAuthService,
              private msg: ApesMessageService) {

    const userInfo = this.userAuthService.getUserInfoObj();
    if(userInfo.userAccount.post.branchObj !== undefined) {
      this.posts = userInfo.userAccount.post.branchObj.origin;
      // if(userInfo.userAccount.post.dept !== undefined) {
      //   this.posts = userInfo.userAccount.post.dept.id;
    }else {
      this.posts = null;
    }

    this.queryData();

    // 初始化 grid
    this.gridOptions = <GridOptions>{
      toolPanelSuppressSideButtons: true,
      suppressExcelExport: true,
      suppressCsvExport: true,
      suppressSizeToFit: true,
      showToolPanel: false,
      pagination: true,
      paginationPageSize: 6,
      rowHeight: 35
    };
    this.gridOptions.localeText = gridConfigService.getLocaleText();

  }

  ngOnInit() {
  }

  /**
   * 功能：按钮事件路由
   * @param data 当前按钮元数据
   */
  onEvent(data:any) {
    if (data instanceof MouseEvent) return;
    if (data) {
      if (data.id === 'action_query') {
        this.queryData();
      }
    }
  }

  queryData() {
    for(let i=0,len= this.reportList.length; i< len; i ++){
      this.reportList[i].rowData = [];
      let api = this.reportList[i].api;
      this.getTablesData(api, i);
    }
  }

  reportList = [
    {
      api: 'ipmMgzzForAppService.getCJHJData',
      width: '100%',
      title: '今日到店未初检环捡',
      rowData: [],
      titleList: [
        {headerName: "接车人", field: 'JIECR', width: 100},
        {headerName: "车牌", field: 'CPHM', width: 100},
        {headerName: "指标", field: 'ZB', width: 240},
        {headerName: "时间", field: 'SJ', width: 120,cellRenderer:this.toHour}
      ]
    },
    {
      api: 'ipmMgzzForAppService.getTSJC',
      width: '100%',
      title: '今日36项未检测',
      rowData: [],
      titleList: [
        {headerName: "接车人", field: 'JIECRMC', width: 100},
        {headerName: "车牌", field: 'CPHM', width: 100},
        {headerName: "施工人", field: 'YGMC', width: 100},
        {headerName: "等待时长", field: 'SJ', width: 120,cellRenderer:this.toHour}
      ]
    }
  ]

  getTablesData(api:string, i:any ) {
    let data = { 'interfaceMethod': api, 'pdata': { 'MDID': this.posts } };
    this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            this.reportList[i].rowData = data;
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

  // 时间转换（配合grid调用）
  toHour(params) {
    let key = params.colDef.field;
    let data =params.data[key];

    let day = Math.floor(data / 1440);
    let hour = day >= 0
      ? Math.floor((data - (day * 1440) ) / 60)
      : Math.floor(data / 60);
    let minute = hour >= 0
      ? Math.floor(data - (day * 1440) - (hour * 60) )
      : Math.floor(data / 60 / 24);

    let time = "";
    if (day > 0) {
      // time = day + "天" + hour + "小时";
      time = day + "天" + hour + "小时" + minute + "分钟";
    }else if(hour > 0){
      time = hour + "小时" + minute + "分钟";
    }else if(minute > 0){
      time = minute + "分钟";
    }
    return time;
  }

}
